package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.BlackListMapper;
import com.tanhua.common.pojo.BlackList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Service 黑名单
 *
 * @author zZZFf
 * @since 2021/7/11 19:12
 */
@Service
public class BlackListService {
    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 分页查询黑名单列表
     *
     * @param userId   用户id
     * @param page     当前页
     * @param pageSize 页大小
     * @return 分页
     */
    public IPage<BlackList> queryBlacklistPage(Long userId, Integer page, Integer pageSize) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        return this.blackListMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
    }

    /**
     * 按 ID 删除黑名单
     *
     * @param blackUserId 移除的黑名单用户id
     * @return Boolean
     */
    public Boolean deleteBlacklistByUserId(Long blackUserId) {
        QueryWrapper<BlackList> deleteWrapper = new QueryWrapper();
        deleteWrapper.eq("black_user_id", blackUserId);

        return this.blackListMapper.delete(deleteWrapper) == 1;
    }
}
